﻿'use strict';

/* ****************************************************************
ManagementServiceProxyDev:
+ Events:
    + after
    + before
    + failed
    + success
**************************************************************** */

/*global module, exports, define, ActiveXObject*/
(function (global, factory) {
    if (typeof exports === 'object') {
        // Node
        module.exports = factory();
    }
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['webServiceProxy.debug'], factory);
    }
}(typeof window !== "undefined" ? window : this, function (WebServiceProxyDebug) {
    var ManagementServiceProxyDebug = WebServiceProxyDebug.extend({
        employersColl: {},
        employersList: [
            {
                id: 1,
                name: 'Nicolas',
                lastName: 'Lareu',
                tel: '4293-3536',
                email: 'nlare2@gmail.com',
                address: 'Macias',
                addressNumber: 673,
                department: '2',
                departmentNumber: 'C',
                locality: 'Adrogue',
                city: 'Alte Brown',
            },
            {
                id: 2,
                name: 'Nicolas2',
                lastName: 'Lareu',
                tel: '4293-3536',
                email: 'nlare2@gmail.com',
                address: 'Macias',
                addressNumber: 673,
                department: '2',
                departmentNumber: 'C',
                locality: 'Adrogue',
                city: 'Alte Brown',
            },
        ],

        initialize: function(options) {
            ManagementServiceProxyDebug.__super__.initialize.apply(this, [options]);

            var that = this,
                fn_each = function(i, item) {
                    that.employersColl[item.id] = item;
                };

            $.each(this.employersList, fn_each);
        },

        addEmployer: function (employer, options) {
            employer.id = Math.floor(Math.random() * 10000000000);

            this.employersList.push(employer);
            this.employersColl[employer.id] = employer;

            this.requestSuccess(
                'addEmployer',
                employer,
                new utils.backend.Response({ data: employer }),
                options);
        },
        getEmployers: function (options) {
            this.requestSuccess(
                'getEmployers',
                null,
                _.clone(this.employersList),
                options);
        },
        updateEmployer: function (employer, options) {
            this.employersColl[employer.id] = employer;

            this.requestSuccess(
                'updateEmployer',
                employer,
                new utils.backend.Response({ data: employer }),
                options);
        },
    });

    return ManagementServiceProxyDebug;
}));